package com.zm.exam.springbootexam.controller;


import com.zm.exam.springbootexam.biz.BizException;
import com.zm.exam.springbootexam.biz.UserBiz;
import com.zm.exam.springbootexam.entry.Result;
import com.zm.exam.springbootexam.entry.User;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    UserBiz userBiz;
    @RequestMapping(path = "reg")
    public Result reg(@Valid @RequestBody UserInfo user, Errors errors){
        System.out.println("user = " + user.toString());
        if (user.getRePwd()==null || user.getRePwd().isBlank()) {
            errors.rejectValue("rePwd","is_empty","确认密码不能为空！");
        }
        if (user.getRePwd().equals(user.getPwd())==false) {
            errors.rejectValue("rePwd","not_equals","确认密码与密码不一致！");
        }
        if (errors.hasErrors()){
           return new Result(0,"注册信息错误！",errors.getAllErrors());
        }

        try {
            userBiz.reg(user);
            return new Result(1,"注册成功",null);
        } catch (BizException e) {
            String field = e.getData().toString();
            errors.rejectValue(field,"??",e.getMessage());
            return new Result(0,"注册信息错误！",errors.getAllErrors());
        }

    }
    @PostMapping("log")
    public Result login(@Valid @RequestBody User user, Errors errors,HttpSession session){
        if (errors.hasFieldErrors("name") || errors.hasFieldErrors("pwd")) {
            List<Object> error = new ArrayList<>();
            error.add(errors.getFieldError("name"));
            error.add(errors.getFieldError("pwd"));
            return new Result(0,"字段验证错误",error);
        }
        try {
            User dbUser = userBiz.login(user);
            dbUser.setPwd(null);
            String phone = dbUser.getPhone().substring(0,3) + "****" + dbUser.getPhone().substring(8);
            dbUser.setPhone(phone);
            session.setAttribute("myinfo",dbUser);
        } catch (BizException e) {
            errors.rejectValue(e.getData().toString(),"name.error",e.getMessage());
            return new Result(0,e.getMessage(),errors.getAllErrors());
        }
        Object requestURI = session.getAttribute("requestURI");
        return new Result(1,"登录成功",requestURI);
    }

    @GetMapping("logout")
    public ModelAndView logout(ModelAndView mav, HttpSession session){
        session.invalidate();
        mav.setViewName("redirect:/");
        return mav;
    }

    @PostMapping("myinfo")
    public Result myinfo(@SessionAttribute(required = false) User myinfo){
       if (myinfo == null){
           return new Result(1,"未登录",null);
       }else {
           return new Result(1,"已登录",myinfo);
       }
    }

    @PostMapping("upload")
    public Result upload(@RequestParam("image") MultipartFile file) throws IOException {
        System.out.println("file.getName() = " + file.getName());
        System.out.println("file.getOriginalFilename() = " + file.getOriginalFilename());
        System.out.println("file.getSize() = " + file.getSize());
        File fileObj = new File("F:\\yuancheng\\javaproject\\s3\\heads\\" + file.getOriginalFilename());
        file.transferTo(fileObj);
        String webPath = "heads/" + file.getOriginalFilename();
        return new Result(1,"上传成功",webPath);
    }
}

@Data
class UserInfo extends User {
    private String rePwd;
}
